package prefuse.tp.svn;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.ItemAction;
import prefuse.action.RepaintAction;
import prefuse.action.animate.ColorAnimator;
import prefuse.action.animate.PolarLocationAnimator;
import prefuse.action.animate.QualityControlAnimator;
import prefuse.action.animate.VisibilityAnimator;
import prefuse.action.assignment.DataSizeAction;
import prefuse.action.assignment.FontAction;
import prefuse.action.layout.CollapsedStackLayout;
import prefuse.action.layout.CollapsedSubtreeLayout;
import prefuse.action.layout.Layout;
import prefuse.action.layout.RandomLayout;
import prefuse.action.layout.graph.BalloonTreeLayout;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.action.layout.graph.FruchtermanReingoldLayout;
import prefuse.action.layout.graph.NodeLinkTreeLayout;
import prefuse.action.layout.graph.RadialTreeLayout;
import prefuse.action.layout.graph.SquarifiedTreeMapLayout;
import prefuse.activity.SlowInSlowOutPacer;
import prefuse.controls.ControlAdapter;
import prefuse.controls.DragControl;
import prefuse.controls.FocusControl;
import prefuse.controls.HoverActionControl;
import prefuse.controls.PanControl;
import prefuse.controls.WheelZoomControl;
import prefuse.controls.ZoomToFitControl;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Table;
import prefuse.data.Tuple;
import prefuse.data.event.TupleSetListener;
import prefuse.data.io.GraphMLReader;
import prefuse.data.query.SearchQueryBinding;
import prefuse.data.search.PrefixSearchTupleSet;
import prefuse.data.search.SearchTupleSet;
import prefuse.data.tuple.DefaultTupleSet;
import prefuse.data.tuple.TupleSet;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.EdgeRenderer;
import prefuse.util.ColorLib;
import prefuse.util.FontLib;
import prefuse.util.GraphicsLib;
import prefuse.util.UpdateListener;
import prefuse.util.display.DisplayLib;
import prefuse.util.display.ItemBoundsListener;
import prefuse.util.ui.JFastLabel;
import prefuse.util.ui.JSearchPanel;
import prefuse.util.ui.UILib;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.InGroupPredicate;
import prefuse.visual.sort.TreeDepthItemSorter;



public class GraphRadial extends Display implements ActionListener {

	private static final long serialVersionUID = -4534308983276208203L;
	public 	static final String DATA_FILE = "/data.xml";
	private String m_label = "label";
	
    static final String tree = "tree";
    static final String treeNodes = "tree.nodes";
    static final String treeEdges = "tree.edges";
    static final String linear = "linear";
    
	static ActionList filter;
	static FontAction fonts; 
	static Layout treeLayout;
    static GraphRadial_nodeRenderer m_nodeRenderer;
    static GraphRadial_edgeRenderer m_edgeRenderer;
    static GraphRadial_edgeColorAction edgeColor;
    static GraphRadial_textColorAction textColor;
    static GraphRadial_dataColorAction dataColorUE;
    static JFrame frame;    
    JSlider slider;
    GraphRadial_sizeAction size;
    
    GraphRadial_jTable jTablePI;
    GraphRadial_jTable jTableUE; 
    GraphRadial_jTable jTableStudent;
    GraphRadial_jTable jTable;
    JScrollPane JspanePI;
    JScrollPane JspaneUE; 
    JScrollPane JspaneStudent;
    JScrollPane Jspane;
    
    public GraphRadial(Graph g, String label) {
    	
        super(new Visualization());
        
        m_label = label;

        // Visualisation
        m_vis.add(tree, g);
        
        m_vis.setInteractive(treeEdges, null, true);
        
        // Renderers
        m_nodeRenderer = new GraphRadial_nodeRenderer(m_label);
        m_edgeRenderer = new GraphRadial_edgeRenderer(EdgeRenderer.RENDER_TYPE_DRAW);
        
        
        DefaultRendererFactory rf = new DefaultRendererFactory(m_nodeRenderer);
        rf.add(new InGroupPredicate(treeEdges), m_edgeRenderer);
        m_vis.setRendererFactory(rf);
               
       
        // Coloration avec palettes de couleurs
        int[] paletteDegrade = new int[] {
        	ColorLib.rgb(250,250,200),
            ColorLib.rgb(250,200,250),
            ColorLib.rgb(250,250,250),
            ColorLib.rgb(250,200,200),
            ColorLib.rgb(250,250,200),
            ColorLib.rgb(250,200,250),
        };
        
        
        //Couleur des noeuds
        ItemAction nodeColor = new GraphRadial_nodeColorAction(treeNodes);
        
        //Couleur du texte
        textColor = new GraphRadial_textColorAction(treeNodes);
        
        /**Ajout de l'action pour la couleur faite en fonction des salaires  
        ColorAction dataColor = new DataColorAction(treeNodes,
          											"Salaire",
        											Constants.NOMINAL,
        											VisualItem.FILLCOLOR,
        											paletteDegrade);*/
        
        dataColorUE = new GraphRadial_dataColorAction(treeNodes,
													"Mention",
													Constants.NOMINAL,
													VisualItem.FILLCOLOR,
													paletteDegrade);       
        
        //Couleur des aretes
        edgeColor = new GraphRadial_edgeColorAction(treeEdges,
                							   VisualItem.STROKECOLOR,
                							   ColorLib.rgb(220,220,220));
        
        //Gestion des fonts amelioree
        fonts = new FontAction(treeNodes, FontLib.getFont("Tahoma", Font.PLAIN, 10));
        fonts.add("_hover", FontLib.getFont("Tahoma", Font.PLAIN, 12));
        fonts.add("ingroup('_focus_')", FontLib.getFont("Tahoma", Font.BOLD, 16));
        fonts.add("ingroup('_search_')", FontLib.getFont("Tahoma", Font.PLAIN, 18));
        
        // Action recolor - permet d'ajouter la visualisation de la couleur
        filter = new ActionList();
        //Permet de definir recolor comme action regoupant les noeuds,aretes,fonts
        //et remplissage
        //  filter.add(nodeColor);
        //  filter.add(textColor);
        //  filter.add(fonts);
        // filter.add(dataColorUE);
        
        size = new GraphRadial_sizeAction(treeNodes, "taille");/*
    	size.setMinimumSize(.5);
    	size.setMaximumSize(5.0);*/
    	m_vis.putAction("size", size);

        // Ajoute les actions a la visualisation
        m_vis.putAction("textColor", textColor);
       	m_vis.putAction("nodeColor", nodeColor);
        m_vis.putAction("recolor", filter);
        
        // Action repaint - permet de faire un repaint global
        ActionList repaint = new ActionList();
        repaint.add(filter);
        repaint.add(new RepaintAction());
        m_vis.putAction("repaint", repaint);
        
        // Action animate paint change
        ActionList animatePaint = new ActionList(1250);
        animatePaint.add(new ColorAnimator(treeNodes));
        animatePaint.add(new RepaintAction());
        
        // Ajoute l' action animate a la visualisation
        m_vis.putAction("animatePaint", animatePaint);
        
        //Creation du Layout
        treeLayout = new GraphRadial_FlexibleLayout(tree);
        /**
         * GraphRadial_FlexibleLayout treeLayout = new GraphRadial_FlexibleLayout(tree);
         * RadialTreeLayout treeLayout = new RadialTreeLayout(tree);
         * NodeLinkTreeLayout treeLayout = new NodeLinkTreeLayout(tree,Constants.ORIENT_LEFT_RIGHT, 50, 0, 8);
         * FruchtermanReingoldLayout  treeLayout = new FruchtermanReingoldLayout (tree); 
         */
        //NULL ForceDirectedLayout treeLayout = new ForceDirectedLayout(tree);
        //NULL BalloonTreeLayout treeLayout = new BalloonTreeLayout(tree);
        //NULL SquarifiedTreeMapLayout treeLayout = new SquarifiedTreeMapLayout(tree);
        //treeLayout.setAngularBounds(Math.PI/2, Math.PI);        
        m_vis.putAction("treeLayout", treeLayout);
        
        CollapsedSubtreeLayout subLayout = new CollapsedSubtreeLayout(treeNodes);
        m_vis.putAction("subLayout", subLayout);
        
        // Action filter - permet d'appliquer les differents filtres des actions
           filter = new ActionList();
        //Permet de mettre en racine le noeud selectionne
        filter.add(new GraphRadial_treeRootAction(tree));
        filter.add(fonts);
        filter.add(treeLayout);
        filter.add(subLayout);
        filter.add(dataColorUE);
        filter.add(textColor);
        filter.add(nodeColor);
        filter.add(edgeColor);
        
        /*
        DataSizeAction size = new DataSizeAction(treeNodes, "Taille");
        filter.add(size);*/
        
        // Ajoute le filter a la visualisation
        m_vis.putAction("filter", filter);

        
        // Action animated transition - permet l'animation du graphe
        ActionList animate = new ActionList(1250);
        animate.setPacingFunction(new SlowInSlowOutPacer());
        animate.setDuration(500);
        animate.add(new QualityControlAnimator());
        animate.add(new VisibilityAnimator(tree));
        animate.add(new PolarLocationAnimator(treeNodes, linear));
        animate.add(new ColorAnimator(treeNodes));
        animate.add(new RepaintAction());
        
        // Ajoute l'action animate a la visualisation
        m_vis.putAction("animate", animate);
        // Permet de definir que animate doit se "jouer" apres filter
        m_vis.alwaysRunAfter("filter", "animate");
        // ------------------------------------------------    
        // Affichage
    	setSize(800,600);
        setItemSorter(new TreeDepthItemSorter());
        addControlListener(new DragControl());
        
        UpdateListener lstnr = new UpdateListener() {
    	    public void update(Object src) {
    	    }
    	    public void componentResized(java.awt.event.ComponentEvent e) {
    		double x = e.getComponent().getWidth();
    		double y = e.getComponent().getHeight();	
    		double min = x<y ? x : y;
    		if (treeLayout instanceof GraphRadial_FlexibleLayout) {
				GraphRadial_FlexibleLayout gfl = (GraphRadial_FlexibleLayout) treeLayout;
				gfl.setRescale(x/min,y/min);
			}      
    		m_vis.run("filter");
    	    }
    	};
        
    	//MAD - adding the brand new control to the display
    	String tt[] = { "Code:" , "Nom:", "Prenom:","Intitule:", "Description:","Mention:"};
    	String ff[] = { "Code" , "Nom", "Prenom","Intitule", "Description","Mention"};

    	GraphRadial_ControlTooltip dctt = new GraphRadial_ControlTooltip(tt,ff);
    	
        /*
         * Zoom et autres listeners
         */
    	addComponentListener(lstnr);
    	addControlListener(dctt);
    	
        addControlListener(new ZoomToFitControl());
        addControlListener(new WheelZoomControl());
        addControlListener(new PanControl());
        addControlListener(new FocusControl(1, "filter"));
        addControlListener(new HoverActionControl("repaint"));

		m_vis.run("size");
        m_vis.run("filter");
        
        // maintain a set of items that should be interpolated linearly
        // this isn't absolutely necessary, but makes the animations nicer
        // the PolarLocationAnimator should read this set and act accordingly
        m_vis.addFocusGroup(linear, new DefaultTupleSet());
        m_vis.getGroup(Visualization.FOCUS_ITEMS).addTupleSetListener(
            new TupleSetListener() {
                public void tupleSetChanged(TupleSet t, Tuple[] add, Tuple[] rem) {
                    TupleSet linearInterp = m_vis.getGroup(linear);
                    if ( add.length < 1 ) return; linearInterp.clear();
                    for ( Node n = (Node)add[0]; n!=null; n=n.getParent() )
                        linearInterp.addTuple(n);
                }
            }
        );
               
        SearchTupleSet search = new PrefixSearchTupleSet();
        m_vis.addFocusGroup(Visualization.SEARCH_ITEMS, search);
        search.addTupleSetListener(new TupleSetListener() {
            public void tupleSetChanged(TupleSet t, Tuple[] add, Tuple[] rem) {
                m_vis.cancel("animatePaint");
                m_vis.run("recolor");
                m_vis.run("animatePaint");
            }
        });
    }
    
    // ------------------------------------------------------------------------
    
    public static void main(String argv[]) {
    	String infile = DATA_FILE;
        String label = "Code";
        if ( argv.length > 1 ) {
            infile = argv[0];
            label = argv[1];
        }
        
        //UILib.setPlatformLookAndFeel();
        
        frame = new JFrame("TP3 VI - DELFOUR - SOULARY");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(demo(infile, label));
        //frame.setAlwaysOnTop(true);
        frame.setJMenuBar(new GraphRadial_jMenu());
        frame.pack();
        frame.setVisible(true);
    }
    
   public static JPanel demo(String datafile, final String label) {
	   Graph g = null;
        
        try {
        	g = new GraphMLReader().readGraph(datafile);
        	new GraphRadial_Affectations().affecterAleatoirement(g);
        } catch ( Exception e ) {
            e.printStackTrace();
            System.exit(1);
        }
        return demo(g, label);
    }

	static JComboBox selectionLayout;
	static Visualization vis;
    @SuppressWarnings("unchecked")
    public static JPanel demo(final Graph g, final String label) {
        // create a new radial tree view
    	String ColumName ="";
    	final GraphRadial gview = new GraphRadial(g, label);
    	vis = gview.getVisualization();
        
        // create a search panel for the tree map
        SearchQueryBinding sq = new SearchQueryBinding(
             (Table)vis.getGroup(treeNodes), label,
             (SearchTupleSet)vis.getGroup(Visualization.SEARCH_ITEMS));
        JSearchPanel search = sq.createSearchPanel();
        search.setShowResultCount(true);
        search.setBorder(BorderFactory.createEmptyBorder(5,5,4,0));
        search.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 10));
        
        final JFastLabel title = new JFastLabel(" ");
        title.setPreferredSize(new Dimension(350, 20));
        title.setVerticalAlignment(SwingConstants.BOTTOM);
        title.setBorder(BorderFactory.createEmptyBorder(3,0,0,0));
        title.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 16));
        
        gview.addControlListener(new ControlAdapter() {
          @SuppressWarnings("static-access")
		public void itemEntered(VisualItem item, MouseEvent e) {
                if (item.canGetString(label))
                    title.setText("Code:" + item.getString(label));
                	if (item.getSourceTuple() instanceof Node) {
                		Node node = (Node) item.getSourceTuple();
						gview.m_edgeRenderer.setItemSelected(node);
						gview.edgeColor.setItemSelected(node);
					}
            }

            @SuppressWarnings("static-access")
			public void itemClicked(VisualItem item, MouseEvent e) {
                if (item.canGetString(label))
                    title.setText("Code:" + item.getString(label));
                	if (item.getSourceTuple() instanceof Node) {
                		Node node = (Node) item.getSourceTuple();
						gview.m_edgeRenderer.setItemSelected(node);
						gview.edgeColor.setItemSelected(node);
						// table 1
						int s = item.getRow();
						gview.jTable.clearSelection();
						gview.Jspane.getVerticalScrollBar().setValue(s*gview.jTable.getRowHeight()-250);
						gview.jTable.addRowSelectionInterval(s, s);
						if(s >= 123) {
							if(s <= 282) {
								s-=123;
								gview.jTableStudent.clearSelection();
								gview.JspaneStudent.getVerticalScrollBar().setValue(s*gview.jTableStudent.getRowHeight()-250);
								gview.jTableStudent.addRowSelectionInterval(s, s);
							} else {
								s-=283;
								gview.jTablePI.clearSelection();
								//gview.JspanePI.getVerticalScrollBar().setValue(s*gview.jTablePI.getRowHeight()-250);
								gview.jTablePI.addRowSelectionInterval(s, s);
							}
						} else {
							gview.jTableUE.clearSelection();
							gview.JspaneUE.getVerticalScrollBar().setValue(s*gview.jTableUE.getRowHeight()-250);
							gview.jTableUE.addRowSelectionInterval(s, s);
						}
					}
            } 
            
            
            public void itemExited(VisualItem item, MouseEvent e) {
                title.setText(null);
            }
        });
        
        /**Ajout du Salaire     
        Iterator it = g.tuples(); g.addColumn("Salaire",Integer.class); Tuple t;        
        while ( it.hasNext() ) {
        	  
        	t = (Tuple)it.next(); int salaire = (int) (1+Math.random()*1000);
        	t.set("Salaire",salaire); salaire=0;
        }*/
                 
        //Creation de la Table
        final Table table = g.getNodeTable();
        
        final DefaultTableModel dtmPI = new DefaultTableModel();
        final DefaultTableModel dtmUE = new DefaultTableModel();
        final DefaultTableModel dtmSTU = new DefaultTableModel();
        final DefaultTableModel dtm = new DefaultTableModel();
        
        final String[] dataPI = new String[1];
        final String[] dataUE = new String[4];
        final String[] dataSTU = new String[3];
        final String[] data  = new String[table.getColumnCount()];

        dtmPI.addColumn(table.getColumnName(0));
        
        dtmSTU.addColumn(table.getColumnName(0));
        dtmSTU.addColumn(table.getColumnName(1));
        dtmSTU.addColumn(table.getColumnName(2));
        
        dtmUE.addColumn(table.getColumnName(0));
        dtmUE.addColumn(table.getColumnName(3));
        dtmUE.addColumn(table.getColumnName(4));
        dtmUE.addColumn(table.getColumnName(5));

        for ( int i=0 ; i<table.getColumnCount() ; i++ ) {
        	dtm.addColumn(table.getColumnName(i));
        }

        Iterator it = g.nodes();

	     while( it.hasNext() ) {
	      	Tuple t2 = (Tuple)it.next();
	    	for ( int i=0 ; i<table.getColumnCount() ; i++ ) {
	    		data[i] = t2.get(i)+"";
				ColumName = table.getColumnName(i);
				if (ColumName.equals("Code")){
					dataUE[0] = t2.get(0)+"";
					dataSTU[0]= t2.get(0)+"";
					if ((t2.get(1)==null)&&(t2.get(3)==null)){
						dataPI[0]= t2.get(0)+"";
						dataSTU[1]= null;
					}
					
				}
				if (ColumName.equals("Nom")){
					if ((t2.get(1))!=null){	
						dataSTU[1] = t2.get(1)+"";
						dataUE[3] = null;
						dataUE[2] = null;
					}
				}
				if (ColumName.equals("Prenom")){
					if ((t2.get(2))!=null){	dataSTU[2] = t2.get(2)+""; }
				}
				if (ColumName.equals("Intitule")){
					if ((t2.get(3))!=null){
						dataUE[1] = t2.get(3)+"";
						dataSTU[1] = null;
					}
				}
				if (ColumName.equals("Description")){
					if ((t2.get(4))!=null){ dataUE[2] = t2.get(4)+"";}
				}
				if (ColumName.equals("Mention")){
					if ((t2.get(5))!=null){ dataUE[3] = t2.get(5)+""; }
				}
	    	}
	    	if (dataUE[3] != null) {
	    			dtmUE.addRow(dataUE);
	    	}
	    	if (dataSTU[1] != null) {
	    			dtmSTU.addRow(dataSTU);
	    	}
	    	if (dataPI[0] != null) {
	    		dtmPI.addRow(dataPI);
	    	}
	    	dtm.addRow(data);
	     }
	     
	    gview.jTablePI 		= new GraphRadial_jTable(dtmPI);
        gview.jTableUE 		= new GraphRadial_jTable(dtmUE); 
        gview.jTableStudent 	= new GraphRadial_jTable(dtmSTU);
        gview.jTable 	= new GraphRadial_jTable(dtm);
        
        gview.JspanePI 		= new JScrollPane(gview.jTablePI);
        gview.JspaneUE 		= new JScrollPane(gview.jTableUE); 
        gview.JspaneStudent 	= new JScrollPane(gview.jTableStudent);
        gview.Jspane 			= new JScrollPane(gview.jTable);
        
        dtm.addTableModelListener( new TableModelListener() {
    		public void tableChanged( TableModelEvent tme)  {
    			gview.jTable.jTable_Changed(tme,dtm,gview.m_vis.getGroup(treeNodes),gview.m_vis,0);
    		}
    	});
        dtmUE.addTableModelListener( new TableModelListener() {
    		public void tableChanged( TableModelEvent tme)  {
    			gview.jTableUE.jTable_Changed(tme,dtm,gview.m_vis.getGroup(treeNodes),gview.m_vis,0);
    		}
    	});
        dtmSTU.addTableModelListener( new TableModelListener() {
    		public void tableChanged( TableModelEvent tme )  {
    			gview.jTableStudent.jTable_Changed(tme,dtmSTU,gview.m_vis.getGroup(treeNodes),gview.m_vis,1);
    		}
    	});
        dtmPI.addTableModelListener( new TableModelListener() {
    		public void tableChanged( TableModelEvent tme )  {
    			gview.jTablePI.jTable_Changed(tme,dtmPI,gview.m_vis.getGroup(treeNodes),gview.m_vis,2);
    		}
    	});

        //Listener de selection
        gview.jTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
        	public void valueChanged( ListSelectionEvent lse ) {
        		gview.jTable.jTable_valueChanged(lse,table,gview.m_vis.getGroup(treeNodes),gview.m_vis,0);
			}
        });
        gview.jTableUE.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
        	public void valueChanged( ListSelectionEvent lse ) {
        		gview.jTableUE.jTable_valueChanged(lse,table,gview.m_vis.getGroup(treeNodes),gview.m_vis,0);
			}
        });
        gview.jTableStudent.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
        	public void valueChanged( ListSelectionEvent lse ) {
        		gview.jTableStudent.jTable_valueChanged(lse,table,gview.m_vis.getGroup(treeNodes),gview.m_vis,1);
			}
        });
        gview.jTablePI.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
        	public void valueChanged( ListSelectionEvent lse ) {
        		gview.jTablePI.jTable_valueChanged(lse,table,gview.m_vis.getGroup(treeNodes),gview.m_vis,2);
			}
        });
        
        //Lisener de la table
       /* gview.jTable.addMouseListener( new MouseListener() {
            public void mouseEntered(MouseEvent me)  {;}
            public void mouseExited(MouseEvent me)   {;}
            public void mousePressed(MouseEvent me)  {;}
            public void mouseReleased(MouseEvent me) {;}
			public void mouseClicked(MouseEvent me)  {;}
        });*/
        
        gview.Jspane.setViewportView(gview.jTable);
        gview.JspaneUE.setViewportView(gview.jTableUE);  
        gview.JspaneStudent.setViewportView(gview.jTableStudent);
        gview.JspanePI.setViewportView(gview.jTablePI);

        selectionLayout = new JComboBox();
		selectionLayout.addItem("GraphRadial_FlexibleLayout");
		selectionLayout.addItem("GraphRadial_FlexibleLayout2");
		selectionLayout.addItem("RadialTreeLayout");
		selectionLayout.addItem("BalloonTreeLayout");
		selectionLayout.addItem("SquarifiedTreeMapLayout");
		selectionLayout.addItem("NodeLinkTreeLayout");
		selectionLayout.addItem("FruchtermanReingoldLayout");
		selectionLayout.addItem("RandomLayout");
		// selectionLayout.addItem("CollapsedStackLayout");
		selectionLayout.addItem("ForceDirectedLayout");
		selectionLayout.addActionListener(gview);
        
        Box box = new Box(BoxLayout.X_AXIS);
        
        box.add(Box.createHorizontalStrut(10));
        box.add(title);
        gview.slider = new JSlider();
        gview.slider.setMinimum(1);
        gview.slider.setValue(100);
        gview.slider.setMaximum(400);
        gview.slider.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				gview.size.setFacteur(gview.slider.getValue()/100.0);
				gview.m_vis.putAction("size", gview.size);
				gview.m_vis.run("filter");
				gview.m_vis.run("size");
			}
        });
        box.add(new JLabel("Taille des noeuds :"));
        box.add(gview.slider);
        box.add(Box.createHorizontalGlue());
        box.add(gview.slider);
        
        
        box.add(Box.createHorizontalGlue());
        box.add(search);
        box.add(Box.createHorizontalGlue());
        box.add(selectionLayout);
        box.add(Box.createHorizontalStrut(3));
        
        
        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel panel 	= new JPanel(new BorderLayout());
                
        tabbedPane.addTab("Fichier XML", gview.Jspane);
        tabbedPane.addTab("Parcours", gview.JspanePI);
        tabbedPane.addTab("Unites d'enseignements", gview.JspaneUE);
        tabbedPane.addTab("Etudiant",gview.JspaneStudent);
        tabbedPane.setVisible(true);
        
        panel.add(gview, BorderLayout.CENTER);
        panel.add(tabbedPane, BorderLayout.EAST);
        panel.add(box, BorderLayout.SOUTH);
        
        Color BACKGROUND = Color.WHITE;
        Color FOREGROUND = Color.DARK_GRAY;
        UILib.setColor(panel, BACKGROUND, FOREGROUND);
        
        return panel;
    }
    
    public static class FitOverviewListener implements ItemBoundsListener {
        private Rectangle2D m_bounds = new Rectangle2D.Double();
        private Rectangle2D m_temp = new Rectangle2D.Double();
        private double m_d = 15;
        public void itemBoundsChanged(Display d) {
            d.getItemBounds(m_temp);
            GraphicsLib.expand(m_temp, 25/d.getScale());
            
            double dd = m_d/d.getScale();
            double xd = Math.abs(m_temp.getMinX()-m_bounds.getMinX());
            double yd = Math.abs(m_temp.getMinY()-m_bounds.getMinY());
            double wd = Math.abs(m_temp.getWidth()-m_bounds.getWidth());
            double hd = Math.abs(m_temp.getHeight()-m_bounds.getHeight());
            if ( xd>dd || yd>dd || wd>dd || hd>dd ) {
                m_bounds.setFrame(m_temp);
                DisplayLib.fitViewToBounds(d, m_bounds, 0);
            }
        }
    }

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource()==selectionLayout){ 
			// gestion des evenements lies a la ComboBox
			String Command = selectionLayout.getSelectedItem().toString();
			Treelayout_Change(Command);
			System.out.println(Command);
		}
	}
	private void Treelayout_Change(String pos) {
        
		filter.remove(treeLayout);
		//System.out.println(Command);
		if(pos.equals("RadialTreeLayout")) {
			treeLayout = new RadialTreeLayout(tree);
		} else if(pos.equals("BalloonTreeLayout")) {
			treeLayout = new BalloonTreeLayout(tree);
		} else if(pos.equals("SquarifiedTreeMapLayout")) {
			treeLayout = new SquarifiedTreeMapLayout(tree);
		} else if(pos.equals("NodeLinkTreeLayout")) {
			treeLayout = new NodeLinkTreeLayout(tree);
	       // treeLayout = new NodeLinkTreeLayout(tree,Constants.ORIENT_LEFT_RIGHT, 50, 0, 8);
		} else if(pos.equals("FruchtermanReingoldLayout")) {
			treeLayout = new FruchtermanReingoldLayout(tree);
		} else if(pos.equals("RandomLayout")) {
			treeLayout = new RandomLayout(tree);
		} else if(pos.equals("CollapsedStackLayout")) {
			treeLayout = new CollapsedStackLayout(tree);
		} else if(pos.equals("ForceDirectedLayout")) {
			treeLayout = new ForceDirectedLayout(tree);
		} else if(pos.equals("GraphRadial_FlexibleLayout")) {
			treeLayout = new GraphRadial_FlexibleLayout(tree);
		} else if(pos.equals("GraphRadial_FlexibleLayout2")) {
			treeLayout = new GraphRadial_FlexibleLayout2(tree);
		} else treeLayout = new RadialTreeLayout(tree);
		filter.add(treeLayout);
		m_vis.putAction("filter", filter);
		m_vis.run("filter");  
	}
} // end of class RadialGraphView
